package com.talent.jiwen_teacher.chatlive;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.abc.live.ui.live.ABCInteractiveActivity;
import com.abcpen.open.api.model.ABCUserMo;
import com.abcpen.open.api.model.RoomMo;
import com.talent.jiwen_teacher.ActivityManager;
import com.talent.jiwen_teacher.LoginActivity;
import com.talent.jiwen_teacher.R;
import com.talent.jiwen_teacher.course.WaitSureFragment;
import com.talent.jiwen_teacher.http.common.ApiFactory;
import com.talent.jiwen_teacher.http.common.HttpUtil;
import com.talent.jiwen_teacher.http.common.ProgressSubscriber;
import com.talent.jiwen_teacher.my.EvaluateActivity;
import com.talent.jiwen_teacher.util.ImageUtil;
import com.talent.jiwen_teacher.util.SPConstant;
import com.talent.jiwen_teacher.util.SPUtil;
import com.wevey.selector.dialog.MDSelectionDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;
import me.jessyan.autosize.internal.CancelAdapt;
import rx.Observable;

import static com.liveaa.livemeeting.sdk.biz.core.ABCLiveSDK.showToast;
import static com.talent.jiwen_teacher.course.CourseWaitFragment.REFRESH_WAIT_COURSE;

/**
 * Created by qyqx on 2017/6/28.
 */

public class InteractiveActivity extends ABCInteractiveActivity implements CancelAdapt {

    private boolean isRecording = true;
    private MDSelectionDialog exitDialog;
    ArrayList exitDialogData = new ArrayList<>();
//    protected RoomMo mRoomMo;
    private String roomId;

    public static final String TIME_FINISH_CLASS = "time.finish.class";


//    @Subscribe
//    public void onEvent(String flag){//免单课程结束通知
//        if (flag.equals(TIME_FINISH_CLASS)){
//            finishClass();
//            Intent intent=new Intent(InteractiveActivity.this, EvaluateActivity.class);
//            intent.putExtra("RoomId",roomId);
//            startActivity(intent);
//            finish();
//        }
//    }


    @Override
    public void onShareViewClick(RoomMo mo) {
        // TODO: 2017/6/28 share
    }

    @Override
    public void onRemoteDoCloseLive() {
        Log.d("zc", "onRemoteDoCloseLive: ");
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        EventBus.getDefault().register(this);
        exitDialogData.add(getString(R.string.abc_temp_leave));
        exitDialogData.add(getString(R.string.abc_leave_class));
        exitDialogData.add(getString(R.string.abc_common_cancel));
        roomId=mRoomMo.room_id;
        if(orderType==1){
//            MyToast.showLong(InteractiveActivity.this,"该订单为试课体验单，体验时间为15分钟，提问将在15分钟后自动结束，请老师注意调整时间");
            showCoursePointDialog();
        }
    }


    /**
     * 课程暂时中断
     */
    @Override
    protected void onMeetingInterrupt() {
        EventBus.getDefault().post(REFRESH_WAIT_COURSE);
        EventBus.getDefault().post(WaitSureFragment.REFRESH_WAIT_SURE);
        finish();
    }

    int count=0;

    /**
     * 老师结束
     */
    private void endClass(final String orderId){

        Map<String, String> map = new HashMap<>();
        map.put("teacherId", SPUtil.stringOut(SPConstant.USER_ID));
        map.put("orderId", orderId);

        Observable observable = ApiFactory.getEducationApiSingleton().teacherFinishClass(SPUtil.stringOut(SPConstant.TOKEN), map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber(InteractiveActivity.this) {
            @Override
            protected void _onNext(Object o) {
                finishClass();
                Intent intent=new Intent(InteractiveActivity.this, EvaluateActivity.class);
                intent.putExtra("RoomId",orderId);
                startActivity(intent);
                finish();
            }

            @Override
            protected void _onError(String message) {
                count++;
                if(count>5){
                    count=0;
                    Toast.makeText(InteractiveActivity.this, message, Toast.LENGTH_SHORT).show();
                    return;
                }else{
                    endClass(roomId);
                }
            }
        },true);
    }

    @Override
    public void upLoadImgAndSendMsg(final String filePath, final int width, final int height, final int pageNo, final String wbId) {
        ImageUtil.upLoadSingleImg(filePath, new ImageUtil.ImgUpLoadListener() {
            @Override
            public void OnSuccess(String successUrl) {
                //发送消息
                if (mWhiteboardFragment != null) {
                    mWhiteboardFragment.sendPhotoImageForUrl(successUrl, filePath , width , height , pageNo , wbId);
                }
            }

            @Override
            public void OnError(String message) {

            }

            @Override
            public void OnListUpSuccess(List<String> successFileList) {

            }
        });
    }

    /**
     * 透传消息
     *
     * @param uid
     * @param data
     */
    @Override
    public void onCMDMsg(ABCUserMo uid, String data) {
        showToast("name" + uid.uname + " data" + data);
    }

    /**
     * 指定透传消息
     *
     * @param uid
     * @param tuid
     * @param data
     */
    @Override
    public void onCMDToUserMsg(ABCUserMo uid, ABCUserMo tuid, String data) {
        showToast("name" + uid.uname + " data" + data);
    }

    @Override
    public void setFinishLoading() {

    }




    /**
     * 踢出用户
     *
     * @param fid
     * @param userId
     * @param kickedUser
     */
    @Override
    public void onKickedOutUser(String fid, String userId, int kickedUser) {

    }

    /**
     * 获取用户信息
     *
     * @param what
     * @param userMos
     */
    @Override
    public void onUsersInfo(int what, List<ABCUserMo> userMos) {

    }

    /**
     * token 验证失败
     */
    @Override
    public void onErrorOpen() {
        super.onErrorOpen();
        Toast.makeText(InteractiveActivity.this,"token验证失败，请重新登录",Toast.LENGTH_SHORT).show();
        SPUtil.clearAll();
        ActivityManager.getAppManager().finishAllActivity();
        startActivity(new Intent(InteractiveActivity.this,LoginActivity.class));
        JPushInterface.setAlias(InteractiveActivity.this , (int)Math.random(), "");
        finish();

    }

    /**
     * 课程结束
     */
    @Override
    protected void onMeetingFinish() {
        endClass(roomId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().unregister(this);
    }
}
